package com.eleventwelve.iris.utilities;

import imageUtil.ImageLoader;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.apache.log4j.Logger;
import org.springframework.util.FileCopyUtils;

public class ImageUtil {

	private static Logger _log = Logger.getLogger(ImageUtil.class);
	private static CacheManager manager = CacheManager.create();
	
	/**
	 * Loads the image from cache if available.
	 * 
	 * @param fullPath
	 * @return
	 */
	public static byte[] loadImage(String fullPath) {
		try {
			File file = new File(fullPath);
			byte[] barray = null;
			Cache imageCache = manager.getCache("UserImageCache");
			if (imageCache == null) {
				// eh-cache is not configured.
				barray = FileCopyUtils.copyToByteArray(file);				
			} else {
				// use eh-cache
				if (imageCache.isElementInMemory(fullPath)) {
					// already in cache
					barray = (byte[]) imageCache.get(fullPath).getObjectValue();
				} else {
					// load from file and add to cache
					if(!file.exists()) {
						_log.error("File [" + fullPath + "] not found.");
					} else {
						barray = FileCopyUtils.copyToByteArray(file);
						imageCache.put(new Element(fullPath, barray));
					}
				}
			}
			return barray;
		} catch (Exception e) { 
			_log.error("Failed to load image.", e);
			return null;
		}
	}

	/**
	 * Checks if image is valid
	 * 
	 * Image must be at least 250 x 250 pixels
	 * File size must not exceed 10 Megabytes
	 * @param inputStream 
	 * 
	 * @param input
	 * @return
	 */
	public static boolean isValidSize(InputStream inputStream) {
		try {
			BufferedImage image = ImageIO.read(inputStream);
			Integer width = image.getWidth();
			Integer height = image.getHeight();
			if(width >= 250 && height >= 250)
				return true;
			else
				return false;
		} catch (IOException e) {
			return false;
		}
	}
	
	/**
	 * 
	 * @author AJ
	 * @throws IOException 
	 */
	public static void createPhotoThumbnails(String filePath) throws IOException {
		
		imageUtil.Image img;
		
		img = ImageLoader.fromFile(filePath);
		imageUtil.Image thumb = img.getResizedToSquare(100, 0.0);
		imageUtil.Image mini = img.getResizedToSquare(50, 0.0);
		imageUtil.Image large = img.getResizedToSquare(250, 0.0);
		
		thumb.writeToFile(new File(ImageUtil.buildFileName(filePath, "_thumb") + ".png"));
		mini.writeToFile(new File(ImageUtil.buildFileName(filePath, "_mini") + ".png"));
		large.writeToFile(new File(ImageUtil.buildFileName(filePath, "_large") + ".png"));
		
	}
	
	/**
	 * Public helper used to concatenate postFix of image
	 */
	public static String buildFileName(String fullPath, String postFix) {
		int fullPathExt = fullPath.lastIndexOf(".");
		return fullPath.substring(0, fullPathExt) + postFix;
	}

}
